package com.itfenghuang.day15;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/*
基于TCP协议的服务端接收数据
 */
public class A02_Server {
    public static void main(String[] args) throws IOException {
//        创建ServerSocket
        ServerSocket st = new ServerSocket(12341);
        System.out.println("监听前");
//        监听连接
//        阻塞,知道监听到客户端连接
        Socket at = st.accept();
        System.out.println("监听后");
//        获取输入流对象
        InputStream is = at.getInputStream();
//        读取
//        读取中文的时候容易出现乱码     解决办法转换流 InputStreamReader isr = new InputStreamReader(is); int i1 = isr.read();
//        InputStreamReader isr = new InputStreamReader(is);
//        int i1 = isr.read();
        byte[] bys = new byte[1024];
        int i;
              while ((i=is.read(bys))!=-1){
                  System.out.println(new String(bys,0,i));
              }
              st.close();
    }
}
